package com.itheima.upload;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
上传图片的 服务器
1.接收客户端上传的图片
2.保存到服务器的本地
3.给客户端 响应一个上传成功
 */
public class Server {
    public static void main(String[] args) throws IOException {
        //2.保存到服务器的本地
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16\\copy\\meinv.jpg"));
        //1.接收客户端上传的图片
        ServerSocket ss = new ServerSocket(8181);
        Socket accept = ss.accept();

        InputStream is = accept.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        byte[] bytes = new byte[1024];
        int  len;
        while ((len=bis.read(bytes))!=-1){
            //bytes  0, len
            bos.write(bytes,0,len);
        }

        //3.给客户端 响应一个上传成功
        //字节流
        OutputStream os= accept.getOutputStream();
        //不能写汉字
       // OutputStreamWriter writer = ;
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        bw.write("上传成功!");
       // accept.shutdownOutput();
        bw.flush();
        //释放资源的代码
       bw.close();
        bos.close();
        ss.close();


    }



}
